/*
 * File:   Mailer.h
 * Author: henrya
 *
 * Created on June 25, 2009, 12:38 AM
 */

#ifndef _MAILER_H
#define	_MAILER_H

#include "Node.h"
#include "SynchronizedPriorityQueue.h"
#include <vector>
using std::vector;

class Message;
class Shell;
class OSScheduler;

class Mailer {
public:
    enum MailerMode {DVR, NON_DVR};
    Mailer(Shell* shell,int size);
    virtual ~Mailer();
    void printRTs();
    void setNetwork(Network* network);
    bool tryAddMessage(Message* message);
    void start();
    void run();
    void setScheduler(OSScheduler* scheduler);
    void wakeUp();
    void setMode(MailerMode mode);
    void interrupt();
    void join();
    bool hasRunned();
private://fields
    SynchronizedPriorityQueue inbox;
    Network* network;

    pthread_t self;
    volatile bool interupted;
    Shell* shell;
    OSScheduler* scheduler;
    MailerMode mode;
    pthread_mutex_t classLock;
    pthread_cond_t classCond;

    bool runned;
private://methods
    bool deliverMsgs();
    bool dvrEnded();
    void goToSleep();
};

#endif	/* _MAILER_H */

